package com.apobates.jforum2.troll.nest.service;

import com.apobates.jforum2.troll.accounts.stub.CommonResponseVo;
import com.apobates.jforum2.troll.accounts.stub.vo.MemberOnlineVo;
import com.apobates.jforum2.troll.accounts.stub.vo.MemberProfileVo;
import com.apobates.jforum2.troll.accounts.stub.vo.MemberRecordVo;
import com.apobates.jforum2.troll.accounts.stub.vo.MemberSocialVo;
import com.apobates.jforum2.troll.accounts.stub.vo.MemberStatsCollectVo;
import com.apobates.jforum2.troll.accounts.stub.vo.MemberVo;
import com.apobates.jforum2.troll.nest.FrontFeignClientConfig;
import com.apobates.jforum2.troll.nest.service.MemberClient.MemberFallbackFactory;
import com.apobates.jforum2.troll.regular.ForumActionEnum;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 会员服务客户端
 * @author xiaofanku
 * @since 20210413
 */
@FeignClient(value="TROLL-GATEWAY",
            contextId="memberClient",
            fallbackFactory=MemberFallbackFactory.class,
            configuration = FrontFeignClientConfig.class
)
@RequestMapping(value="/member")
public interface MemberClient {
    //1
    @GetMapping("/{id}")
    ResponseEntity<MemberVo> get(@PathVariable("id")long memberId);
    
    //2:个人头像
    @GetMapping("/{id}/avatar")
    ResponseEntity<String> getAvatar(@PathVariable("id") long memberId);
    
    //3:登录
    @PostMapping(value="/checkIn", consumes="application/x-www-form-urlencoded")
    ResponseEntity<MemberVo> login(
            @RequestParam("names") String names,
            @RequestParam("password") String password,
            @RequestParam("token") String token,
            @RequestParam("ip")String ipAddr);
    
    //4:注销
    @PostMapping(value="/checkOut", consumes="application/x-www-form-urlencoded")
    ResponseEntity<String> logout(
            @RequestParam("id") long memberId,
            @RequestParam("names") String names,
            @RequestParam("token") String token,
            @RequestParam("ip")String ipAddr);
    
    //5:注册
    @PostMapping(value="/registe", consumes="application/x-www-form-urlencoded")
    ResponseEntity<MemberVo> registe(
            @RequestParam("names") String names,
            @RequestParam("password") String password,
            @RequestParam(value="nickname", required=false, defaultValue="-") String nickname,
            @RequestParam(value="invite", required=false, defaultValue="") String inviteCode,
            @RequestParam("token") String token,
            @RequestParam("ip")String ipAddr);
    
    //6:检查注册邀请码是否可用
    @GetMapping("/invite/{code}")
    ResponseEntity<CommonResponseVo> getInviteCode(@PathVariable("code") String inviteCode);
    
    //7:检查会员帐号是否可用
    @GetMapping("/detection/{names}")
    ResponseEntity<String> getUnionNames(@PathVariable("names") String names);
    
    //8:会员在线记录
    @GetMapping("/online/{member}")
    ResponseEntity<MemberOnlineVo> getOnlineRecord(@PathVariable("member") long memberId);
    
    //9:统计信息(actionLogClient实现)
    //@GetMapping("/stats/{member}")
    //ResponseEntity<ThreadsStatsCollectVo> getActionStats(@PathVariable("member") long memberId);
    
    //10:保存在线记录
    @PostMapping(value="/online/create", consumes="application/json; charset=utf-8")
    ResponseEntity<String> createOnlineRecord(@RequestBody MemberOnlineVo mov);
    
    //11:个人信息(通过txt/传递)
    @PostMapping(value="/{id}/profile/form", consumes="application/x-www-form-urlencoded")
    ResponseEntity<MemberProfileVo> calcProfileByForm(@PathVariable("id") long memberId, @RequestParam("stats")String topicStatsJsonString);
    //11:个人信息(通过Json/传递)
    @PostMapping(value="/{id}/profile/json", consumes="application/json; charset=utf-8")
    ResponseEntity<MemberProfileVo> calcProfileByJson(@PathVariable("id") long memberId, @RequestBody Map<ForumActionEnum, Long> topicAction);
    
    
    //12:编辑个人信息
    @PutMapping(value="/{id}/profile/modify", consumes="application/x-www-form-urlencoded")
    ResponseEntity<String> editProfile(
            @PathVariable("id") long memberId,
            @RequestParam("signature") String signatureContent,
            @RequestParam("nickname") String nickname,
            @RequestParam("token") String token,
            @RequestParam("ip")String ipAddr);
    
    //13:编辑密码
    @PutMapping(value="/{id}/password/modify", consumes="application/x-www-form-urlencoded")
    ResponseEntity<String> editPassword(@PathVariable("id") long memberId,
                        @RequestParam("original") String oldPswd,
                        @RequestParam("current") String newPswd,
                        @RequestParam("token") String token,
                        @RequestParam("ip")String ipAddr);
    
    //14:重置会员密码
    @PutMapping(value="/{id}/password/reset", consumes="application/x-www-form-urlencoded")
    ResponseEntity<String> resetPassword(@PathVariable("id") long memberId,
                         @RequestParam("current") String newPswd,
                         @RequestParam("token") String token,
                         @RequestParam("ip")String ipAddr);
    
    //15:编辑头像
    @PutMapping(value="/{id}/avatar/modify", consumes="application/x-www-form-urlencoded")
    ResponseEntity<String> editAvatar(@PathVariable("id") long memberId,
                       @RequestParam("avatar") String avtarFileName,
                       @RequestParam("app_domain") String appLocalDomain,
                       @RequestParam("app_avatar_direct") String avatarDefaultDirect,
                       @RequestParam("token") String token,
                       @RequestParam("ip")String ipAddr);
    //16:社会化信息
    @GetMapping("/{id}/social")
    ResponseEntity<MemberSocialVo> getSocial(@PathVariable("id") long memberId);
    
    //17:查询社会化信息
    @PostMapping(value="/social/query", consumes="application/x-www-form-urlencoded")
    ResponseEntity<MemberSocialVo> getSocialByMail(@RequestParam("names") String names, @RequestParam("mail") String email);
    
    //18:编辑社会化信息
    @PutMapping(value="/{id}/social/modify", consumes="application/x-www-form-urlencoded")
    ResponseEntity<String> editSocial(
            @PathVariable("id")long memberId,
            @RequestParam("names") String names,
            @RequestParam("record") long recordId,
            @RequestParam("weibo") String weibo,
            @RequestParam("weixin") String weixin,
            @RequestParam("qq") String qq,
            @RequestParam("mail") String email,
            @RequestParam("token") String token,
            @RequestParam("ip")String ipAddr);
    
    //19:新增社会化信息
    @PostMapping(value="/{id}/social/create", consumes="application/x-www-form-urlencoded")
    ResponseEntity<MemberSocialVo> newSocial(
            @PathVariable("id")long memberId,
            @RequestParam("names") String names,
            @RequestParam("weibo") String weibo,
            @RequestParam("weixin") String weixin,
            @RequestParam("qq") String qq,
            @RequestParam("mail") String email,
            @RequestParam("token") String token,
            @RequestParam("ip")String ipAddr);
    
    //20:社区标兵(actionLogClient实现)
    //@GetMapping("/model")
    //List<MemberModelVo> getBrandMemberModel(@RequestParam(value="size", required=false, defaultValue="10") int showSize);
    
    //21:底部的会员统计汇总
    @GetMapping("/collect")
    ResponseEntity<MemberStatsCollectVo> getBoardStatsCollect(@RequestParam(value="cycle", required=false, defaultValue="30") int statsCycle);
    
    //22:上次登录记录
    @GetMapping("/login/lastrecord/{id}/{names}")
    ResponseEntity<MemberRecordVo> getLastLoginRecord(@PathVariable("id") long memberId, @PathVariable("names") String names);
    //23:
    @GetMapping("/actionrecord/{names}")
    List<MemberRecordVo> getActionRecords(@PathVariable("names") String names, @RequestParam("size") int showSize);
    
    //27:获取客户端IP
    @GetMapping("/ip/query")
    Map<String,String> getClientRequestIp(HttpServletRequest request);
    
    //28:查看多个会员信息
    @GetMapping("/multi")
    List<MemberVo> getMulti(@RequestParam("ids") String memberIdString);
    
    @Component
    static class MemberFallbackFactory implements FallbackFactory<MemberClientFallback> {
        @Override
        public MemberClientFallback create(Throwable cause) {
            return new MemberClientFallback();
        }
    }
    static class MemberClientFallback implements MemberClient{

        @Override
        public ResponseEntity<MemberVo> get(long memberId) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<String> getAvatar(long memberId) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public ResponseEntity<MemberVo> login(String names, String password, String token, String ipAddr) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<String> logout(long memberId, String names, String token, String ipAddr) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<MemberVo> registe(String names, String password, String nickname, String inviteCode, String token, String ipAddr) {
            return ResponseEntity.badRequest().build();
        }

        @Override
        public ResponseEntity<CommonResponseVo> getInviteCode(String inviteCode) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<String> getUnionNames(String names) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<MemberOnlineVo> getOnlineRecord(long memberId) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<String> createOnlineRecord(MemberOnlineVo mov) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<String> editProfile(long memberId, String signatureContent, String nickname, String token, String ipAddr) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<String> editPassword(long memberId, String oldPswd, String newPswd, String token, String ipAddr) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<String> resetPassword(long memberId, String newPswd, String token, String ipAddr) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<String> editAvatar(long memberId, String avtarFileName, String appLocalDomain, String avatarDefaultDirect, String token, String ipAddr) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<MemberSocialVo> getSocial(long memberId) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<MemberSocialVo> getSocialByMail(String names, String email) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<String> editSocial(long memberId, String names, long recordId, String weibo, String weixin, String qq, String email, String token, String ipAddr) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<MemberSocialVo> newSocial(long memberId, String names, String weibo, String weixin, String qq, String email, String token, String ipAddr) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<MemberStatsCollectVo> getBoardStatsCollect(int statsCycle) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<MemberRecordVo> getLastLoginRecord(long memberId, String names) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public List<MemberRecordVo> getActionRecords(String names, int showSize) {
            return Collections.emptyList();
        }

        @Override
        public Map<String, String> getClientRequestIp(HttpServletRequest request) {
            return Collections.emptyMap();
        }

        @Override
        public List<MemberVo> getMulti(String memberIdString) {
            return Collections.emptyList();
        }

        @Override
        public ResponseEntity<MemberProfileVo> calcProfileByForm(long memberId, String topicStatsJsonString) {
            return ResponseEntity.notFound().build();
        }

        @Override
        public ResponseEntity<MemberProfileVo> calcProfileByJson(long memberId, Map<ForumActionEnum, Long> topicAction) {
            return ResponseEntity.notFound().build();
        }

    }
}